
<!-- 弃用，微信小程序有bug，h5能正常使用 -->
<template>
	<uni-file-picker :imageStyles="imageStyles" :v-model="imageValue" file-mediatype="image" :limit="limit"
		file-extname="png,jpg,jepg" :sourceType="sourceType" @select="selectFile" @delete="deleteFile" />

</template>

<script>
	import {
		image_base_url
	} from '@/api/config.js'
	export default {
		name: "MyImageUpload",
		props: {
			imageStyles: {
				type: Object,
				default: () => {
					return {
						width: '200rpx',
						height: '200rpx'
					}
				}
			},
			sourceType: {
				type: Array,
				default: () => ['album', 'camera'],
			},
			limit: {
				type: Number,
				default: 1
			}
		},
		data() {
			return {
				imageBaseUrl: image_base_url,
				imageValue: '',
				img: '',
			}
		},
		methods: {
			selectFile(e) {
				// h5上传-需要文件file对象
				//const filePath = e.tempFiles[0].file.path;
				// 微信小程序上传-需要微信临时提供临时地址
				const filePath = e.tempFilePaths[0];
				uni.uploadFile({
					url: this.imageBaseUrl + '/common/upload',
					name: 'file',
					filePath: filePath,
					header: {
						//h5 去掉content-type
						'content-type': 'multipart/form-data',
						'Authorization': 'Bearer ' + this.$store.state.user.token
					},
					success: (res) => {
						let data = JSON.parse(res.data)
						this.img = data.fileName
					},
					error: (res) => {},
				})
			},
			deleteFile(e) {
				this.$http({
					url: '/common/fileDelete',
					method: 'post',
					data: {
						path: this.img,
					}
				}).then(res => {

				})
			}
		}
	}
</script>

<style lang="scss">
	
</style>